const install = (Vue, vm) => {
	Vue.prototype.navTo = (url, data='',title='') => {
		if (data!==''){
			vm.$store.commit("com/setRouterData", data)
		}
		if (title!==''){
			vm.$store.commit("com/setTitle", title)
		}

		uni.navigateTo({
			url
		})
	}

	Vue.prototype.showToast = (title,duration=3000) => {
		uni.showToast({
			title,
			icon: "none",
			duration:duration
		})
	}

	Vue.prototype.nullStr = (str) => {
		return str?str:''
	}

	Vue.prototype.nullStrZ = (str) => {
		return str?str:0
	}

	Vue.prototype.dictFilter = (datas) => {
		let obj = {};
		datas.forEach(item => {
			obj[item.dictValue] = {
				txt: item.dictLabel,
				val: item.dictValue
			}
		})
		return obj;
	}

	Vue.prototype.fileterByEnName = (datas) => {
		let obj = {};
		return obj;
	}

//list 是已有的数据，search 是模糊搜索的关键字
	Vue.prototype.$fuzzySearch = (list,search)=>{
		let data = [];
		if(list.length != 0 && search){
			let str = `\S*${search}\S*`;
			let reg = new RegExp(str,'i');//不区分大小写
			list.map(item => {
				if(reg.test(item)){
					data.push(item);
				}
			})
		}
		return data;
	}


	Vue.prototype.getStatusBarHeight = () => {
		let statusBarHeight = 0
		const info = uni.getSystemInfoSync()
		// 设置状态栏高度（H5顶部无状态栏小程序有状态栏需要撑起高度）
		statusBarHeight = info.statusBarHeight
		// 除了h5 app mp-alipay的情况下执行
		// #ifndef H5 || APP-PLUS || MP-ALIPAY
		// 获取胶囊的位置
		const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
		console.log("menuButtonInfo", menuButtonInfo);
		// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
		statusBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info
			.statusBarHeight)
		// #endif
		statusBarHeight *= 2
		return statusBarHeight;
	}

	Vue.prototype.isEmpty = (val) => {
		// null or undefined
		if (val == null) return true;

		if (typeof val === 'boolean') return false;

		if (typeof val === 'number') return !val;

		if (val instanceof Error) return val.message === '';

		switch (Object.prototype.toString.call(val)) {
			// String or Array
			case '[object String]':
			case '[object Array]':
				return !val.length;
				// Map or Set or File
			case '[object File]':
			case '[object Map]':
			case '[object Set]': {
				return !val.size;
			}
			// Plain Object
			case '[object Object]': {
				return !Object.keys(val).length;
			}
		}

		return false;
	}

	Vue.prototype.imgFilter = function(Array,attr='id') {
		const Obj = {}
		Array.filter(item => {
			Obj[item[attr]] = item
		})
		return Obj
	}

	Vue.prototype.imgFilterUrl = function(Array,attr='id') {
		const Obj = {}
		Array.filter(item => {
			Obj[item[attr]] = item.fileUrl
		})
		return Obj
	}

	Vue.prototype.imgFilterByAttrVal = function(Array,attr='fileName',val='') {
		const list=[]
		Array.filter(item => {
			if (item[attr]===val){
				list.push(item)
			}
		})
		return list
	}

	Vue.prototype.imgFilterByAttrValUrl = function(Array,attr='fileName',val='') {
		const list=[]
		Array.filter(item => {
			if (item[attr]===val){
				list.push(item.fileUrl)
			}
		})
		return list
	}


	Vue.prototype.toTree = (data) => {
		let result = []
		if (!Array.isArray(data)) {
			return result
		}
		data.forEach(item => {
			delete item.children;
		});
		let map = {};
		data.forEach(item => {
			map[item.id] = item;
		});
		data.forEach(item => {
			let parent = map[item.parentId];
			if (parent) {
				(parent.children || (parent.children = [])).push(item);
			} else {
				result.push(item);
			}
		});
		return result;
	}

	/**
	 * todo(目前暂时只有一级，返回的数据是当前有的商品的品类，之后两级的时候可能改)
	 * 构造分类两级树
	 */
	Vue.prototype.toCategoryTree = (data, parentId, valueName, children) => {
		let config = {
			parentId: parentId || 'parentId',
			valueName: valueName || parentId,
			childrenList: children || 'children'
		};

		var tree = [];
		var tagList = []
		var tagMap = new Map();
		for (let item of data) {
			tagList.push(item[config.parentId])
		}
		tagList = new Set(tagList);

		return tagList;
	}

	Vue.prototype.msgSuccess = function (msg) {
		this.$refs.uToast.show({ title: msg, type: 'success',position:'top' })
	}

	Vue.prototype.msgError = function (msg) {
	  this.$refs.uToast.show({ title: msg, type: 'error',position:'top' })
	}

	Vue.prototype.msgWarn = function (msg) {
	  this.$refs.uToast.show({ title: msg, type: 'warning',position:'top' })
	}

}

export default {
	install
}
